package contacts.controller;

import java.awt.event.ActionEvent;
import contacts.view.ContactsDialog;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import contacts.po.Room;
import contacts.po.Student;
import contacts.service.ContactsService;
import contacts.view.ContactsFrame;
import contacts.view.FunctionFrame;
import contacts.view.LoginFrame;
import contacts.view.RoomDialog;

public class ContactsControllerListener implements ActionListener {
	private ContactsFrame contactsFrame;
	private ContactsDialog contactsDialog;
	private ContactsService contactsService;
	private RoomDialog roomDialog;
	private LoginFrame loginFrame;

	public ContactsControllerListener() {
		contactsService = new ContactsService();
	}

	/**
	 * 事件处理 实现用户登录、联系人的增删改查和宿舍的增加功能
	 *
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// 登录用户操作
		if ("addLoginDialog".equals(e.getActionCommand())) {
			String nameInput = loginFrame.getNameTextField().getText().trim();
			String passWordInput = loginFrame.getPassWordTextField().getText().trim();
			if (LoginFrame.name.equals(nameInput) && LoginFrame.passWord.equals(passWordInput)) {
				JOptionPane.showMessageDialog(loginFrame, "登录成功，欢迎" + LoginFrame.name + "使用小蜻蜓通讯录");
				loginFrame.dispose();
				ContactsFrame win = new ContactsFrame();
				win.setTitle("小蜻蜓通讯录");
			} else {
				JOptionPane.showMessageDialog(loginFrame, "登录失败,账号或密码错误", "请重新输入账号或密码", JOptionPane.ERROR_MESSAGE);
			}
		}
		// 打开新增学生对话框
		else if ("addStudentDialog".equals(e.getActionCommand())) {
			contactsDialog = new ContactsDialog(contactsFrame, "添加联系人", null);
		}
		// 新增学生提交
		else if ("addPost".equals(e.getActionCommand())) {
			Student newStudent = null;
			newStudent = this.getStudentInstance();
			if (newStudent != null) {
				contactsService.addStudent(newStudent);
				JOptionPane.showMessageDialog(contactsFrame, "添加" + newStudent.getStudentId() + "号联系人成功！");
				contactsDialog.dispose();
				contactsFrame.refresh(contactsService.findAllStudent());
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "添加联系人失败！");
			}
		}
		// 打开查询对话框
		else if ("searchStudentDialog".equals(e.getActionCommand())) {
			List<Student> studentList = new ArrayList<Student>();
			for (Student student : contactsService.findAllStudent()) {
				studentList.add(student);
			}
			String str = JOptionPane.showInputDialog(contactsFrame, "输入同学编号");
			if (str != null && !"".equals(str)) {
				if (str.matches("^[1-9][0-9]*([.][0-9]{1,2})?$")) {
					int studentId = Integer.parseInt(str);
					Student student = new Student();
					student.setStudentId(studentId);
					List<Student> result = contactsService.searchByStudentId(studentList, student);
					contactsFrame.showSearchResult(result);
				} else {
					JOptionPane.showMessageDialog(contactsFrame, "输入的不是数字", "输入错误", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		// 打开修改对话框
		else if ("updateStudentDialog".equals(e.getActionCommand())) {
			int isbn = contactsFrame.getIsbn();
			if (isbn < 0) {
				JOptionPane.showMessageDialog(contactsFrame, "请选择一行数据！");
			} else {
				Student student = contactsService.findAllStudent().get(contactsFrame.getIsbn());
				contactsDialog = new ContactsDialog(contactsFrame, "修改联系人", student);
			}
		}
		// 修改提交
		else if ("updatePost".equals(e.getActionCommand())) {
			Student updatedStudent = this.getStudentInstance();
			if (updatedStudent != null) {
				contactsService.updateStudent(updatedStudent);
				JOptionPane.showMessageDialog(contactsFrame, updatedStudent.getStudentId() + "号联系人修改成功！");
				contactsDialog.dispose();
				contactsFrame.refresh(contactsService.findAllStudent());
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "更新联系人失败！");
			}
		}
		// 删除联系人操作
		else if ("deleteStudent".equals(e.getActionCommand())) {
			int isbn = contactsFrame.getIsbn();
			if (isbn < 0) {
				JOptionPane.showMessageDialog(contactsFrame, "请选择一行数据！");
			} else {
				Student student = contactsService.findAllStudent().get(contactsFrame.getIsbn());
				contactsService.deleteStudent(student.getStudentId());
				JOptionPane.showMessageDialog(contactsFrame, "删除" + student.getStudentId() + "号联系人成功！");
				contactsFrame.refresh(contactsService.findAllStudent());
			}

		}
		// 刷新联系人列表窗口
		else if ("refreshContactsFrame".equals(e.getActionCommand())) {
			contactsFrame.refresh(contactsService.findAllStudent());
		}
		// 打开新增宿舍对话框
		else if ("addRoomDialog".equals(e.getActionCommand())) {
			roomDialog = new RoomDialog(contactsFrame, "添加宿舍", null);
		}
		// 新增宿舍提交
		else if ("addRoomPost".equals(e.getActionCommand())) {
			Room newRoom = null;
			newRoom = this.getRoomInstance();
			if (newRoom != null) {
				contactsService.addRoom(newRoom);
				JOptionPane.showMessageDialog(contactsFrame, "添加" + newRoom.getRoomNo() + "号宿舍成功！");
				roomDialog.dispose();
			}
		}
	}

	private Room getRoomInstance() {
		int roomNo = 0;
		String roomName = null;
		Room room = null;
		String roomNoInput = roomDialog.getRoomNoTextField().getText().trim();
		if (roomNoInput != null && !"".equals(roomNoInput)) {
			if (roomNoInput.matches("^[1-9][0-9]*([.][0-9]{1,2})?$")) {
				roomNo = Integer.parseInt(roomNoInput);
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "宿舍号必须是数字类型", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "宿舍号不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String roomNameInput = roomDialog.getRoomNameTextField().getText().trim();
		if (roomNameInput != null && !"".equals(roomNameInput)) {
			roomName = roomNameInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "宿舍名称不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		if (roomNo > 0 && roomName != null) {
			room = new Room(roomNo, roomName);
		}
		return room;
	}

	private Student getStudentInstance() {
		String studentNo = null;
		int studentId = 0;
		int roomNo = 0;
		String studentName = null;
		// Date date = null;
		// java.sql.Date sqlDate = null;
		String telephone = null;
		String email = null;
		String birthPlace = null;
		String address = null;
		Room room = null;
		Student student = null;
		String studentIdInput = contactsDialog.getStudentIdTextField().getText().trim();
		if (studentIdInput != null && !"".equals(studentIdInput)) {
			if (studentIdInput.matches("^[1-9][0-9]*([.][0-9]{1,2})?$")) {
				studentId = Integer.parseInt(studentIdInput);
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "编号必须是数字类型", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "编号不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String studentNoInput = contactsDialog.getStudentNoTextField().getText().trim();
		if (studentNoInput != null && !"".equals(studentNoInput)) {
			studentNo = studentNoInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "学号不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}

		String studentNameInput = contactsDialog.getStudentNameTextField().getText().trim();
		if (studentNameInput != null && !"".equals(studentNameInput)) {
			studentName = studentNameInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "名字不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}

//		String dateInput = contactsDialog.getBirthdayTextField().getText().trim();
//		if (dateInput != null && !"".equals(dateInput)) {
//				studentId = Integer.parseInt(studentIdInput);
//				SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
//			try {
//				date = new SimpleDateFormat("yyyy-MM-dd").parse(dateInput);
//				sqlDate = new java.sql.Date(date.getTime());
//			} catch (ParseException e) {
//				JOptionPane.showMessageDialog(contactsFrame, "生日类型错误", "输入错误", JOptionPane.ERROR_MESSAGE);
//			}
//		} else {
//			JOptionPane.showMessageDialog(contactsFrame, "生日不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
//		}
		String telephoneInput = contactsDialog.getTelephoneTextField().getText().trim();
		if (telephoneInput != null && !"".equals(telephoneInput)) {
			telephone = telephoneInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "电话不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String emailInput = contactsDialog.getEmailTextField().getText().trim();
		if (emailInput != null && !"".equals(emailInput)) {
			email = emailInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "电子邮件不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String birthPlaceInput = contactsDialog.getBirthPlaceTextField().getText().trim();
		if (birthPlaceInput != null && !"".equals(birthPlaceInput)) {
			birthPlace = birthPlaceInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "籍贯不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String addressInput = contactsDialog.getAddressTextField().getText().trim();
		if (addressInput != null && !"".equals(addressInput)) {
			address = addressInput;
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "地址不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String roomNoInput = contactsDialog.getRoomNoTextField().getText().trim();
		if (roomNoInput != null && !"".equals(roomNoInput)) {
			if (roomNoInput.matches("^[1-9][0-9]*([.][0-9]{1,2})?$")) {
				roomNo = Integer.parseInt(roomNoInput);
				room = contactsService.getRoom(roomNo);
			} else {
				JOptionPane.showMessageDialog(contactsFrame, "宿舍号必须是数字类型", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(contactsFrame, "宿舍号不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		if (studentId > 0 && studentNo != null && studentName != null && roomNo > 0 && telephone != null
				&& email != null && birthPlace != null && address != null && room != null) {
			student = new Student(studentId, studentNo, studentName, telephone, email, birthPlace, address, room);
		}
		return student;
	}

	public ContactsService getContactsService() {
		return contactsService;
	}

	public void setStudentAddDialog(ContactsDialog contactsDialog) {
		this.contactsDialog = contactsDialog;
	}

	public void setContactsFrame(ContactsFrame contactsFrame) {
		this.contactsFrame = contactsFrame;

	}

	public void setRoomAddDialog(RoomDialog roomDialog) {
		this.roomDialog = roomDialog;

	}

	public void setLoginFrame(LoginFrame loginFrame) {
		this.loginFrame = loginFrame;

	}
}
